package org.example.dubbo.nativeimage.consumer;

import org.apache.dubbo.config.annotation.DubboReference;
import org.example.dubbo.nativeimage.DemoService;
import org.example.dubbo.nativeimage.HelloRequest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

@SpringBootApplication(scanBasePackages = {"org.example.dubbo.nativeimage.consumer"})
public class NativeDemoConsumerApplication {
    @DubboReference
    private DemoService demoService;

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(NativeDemoConsumerApplication.class, args);
        NativeDemoConsumerApplication application = context.getBean(NativeDemoConsumerApplication.class);
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        System.out.println("dubbo consumer application started, The time taken to start the application is "
                + (System.currentTimeMillis() - runtimeMXBean.getStartTime()) + " ms");
        long startCallTime = System.currentTimeMillis();
        String result = application.doSayHello("world");
        System.out.println("The time taken for the first call is " + (System.currentTimeMillis() - startCallTime) + " ms");
        System.out.println("result: " + result);
    }

    public String doSayHello(String name) {
        return demoService.sayHello(new HelloRequest(name)).getResponse();
    }
}
